package com.example.sync.handler;

import com.example.sync.po.SysASyncJob;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chentao
 * @version 1.0
 * @description: TODO
 * @date 2025/3/22 下午2:44
 */
@Service
public class SysASyncJobHandlerImpl implements SysASyncJobHandler {

    @Resource
    private List<IHandler> handlerList = new ArrayList<>();

    @Override
    public IHandler getHandler(SysASyncJob sysASyncJob) {
        IHandler iHandler = null;
        for (IHandler handler : handlerList) {
            if (handler.isMatch(sysASyncJob)) {
                iHandler = handler;
            }
        }
        return iHandler;
    }

    @Override
    public void handle(SysASyncJob sysASyncJob) throws Exception {
        IHandler handler = null;
        try {
            handler = getHandler(sysASyncJob);
            handler.handle(sysASyncJob);
            handler.success(sysASyncJob);
        } catch (Exception e) {
            handler.failure(sysASyncJob);
            throw e;
        }
    }
}
